// 对axios进行二次封装
import axios from 'axios'
// 引入进度条
import nprogress from 'nprogress'

const requests = axios.create({
  // 基础路径，路径当中会出现api
  baseURL: '/api',
  timeout: 5000
})
import store from '@/store/index.js'
// 请求拦截器
requests.interceptors.request.use((config) => {
  if (store.state.detail.uuid_token) {
    config.headers.userTempId = store.state.detail.uuid_token
  }
  // 给请求头 header添加token
  if (store.state.user.token) {
    config.headers.token = store.state.user.token
  }
  nprogress.start()

  return config
})
// 响应拦截器
requests.interceptors.response.use((res) => {
  nprogress.done()
  return res.data
}, (error) => {
  //响应失败的回调函数
  return Promise.reject(new Error('false'))
})
// 对外暴露
export default requests
